package com.taptech.doufu.util;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

public class CoverageChecker {
    private static final String TAG = "CoverageChecker";
    private static CoverageChecker INSTANCE;
    /**
     * 是否退出了当前 APP 的标志
     * */
    private boolean isQuit = false;
    // Application Context，防止内存泄漏
    private Context mContext;
    // 延时事件
    private Runnable r;
    // 延时事件发射器
    private Handler handler;
    // 单例模式
    private CoverageChecker(Context context) {
        this.mContext = context.getApplicationContext();
        handler = new Handler(Looper.getMainLooper());
        r = new Runnable() {
            @Override
            public void run() {
                if(isQuit()) {
                    Log.d(TAG, "app is covered, show notify");
                    showCoveredHint();
                }
            }
        };
    }
    public static CoverageChecker getInstance(Context context) {
        if(INSTANCE == null) {
            synchronized (CoverageChecker.class) {
                if(INSTANCE == null) {
                    INSTANCE = new CoverageChecker(context);
                }
            }
        }
        if(INSTANCE.mContext == null) {
            INSTANCE.mContext = context.getApplicationContext();
        }
        return INSTANCE;
    }

    public boolean isQuit() {
        return isQuit;
    }

    public void setQuit(boolean isQuit) {
        this.isQuit = isQuit;
    }

    /**
     * 退出界面时，延迟通知
     *
     * @param activity
     * */
    public synchronized void delayNotify(Activity activity) {
        // 不需要提示，则返回
        if(!isNeedNotify(activity)) {
            return;
        }
        setQuit(true);
        // 先移除已有的
        handler.removeCallbacks(r);
        handler.postDelayed(r, 500);
    }

    /**
     * 进入界面时，移除通知
     * */
    public synchronized void removeNotify() {
        if(isQuit()) {
            setQuit(false);
            handler.removeCallbacks(r);
        }
    }

    /**
     * 判断是否需要提示退出 APP
     * */
    public synchronized boolean isNeedNotify(Activity activity) {
        if(activity == null) {
            Log.w(TAG, "activity == null, not notify");
            return false;
        }
        String actName = activity.getClass().getName();
        if(TextUtils.isEmpty(actName)) {
            Log.w(TAG, "activity name is null, not notify");
            return false;
        }
        // 登录、注册、密码、用户信息等敏感界面才提示，其他界面不提示
//        return actName.contains("MainHomeActivity")          // 登录相关界面
//                || actName.contains("register")       // 注册相关界面
//                || actName.contains("password")       // 密码相关界面
//                || actName.contains("userinfo");      // 信息相关界面
        return actName.contains("Activity");
    }

    /**
     * 当应用被覆盖时，显示提示
     * */
    public void showCoveredHint() {
        if(mContext == null) {
            Log.d(TAG, "showCoveredHint---mContext == null");
            return;
        }
//        Toast.makeText(mContext, "豆花阅读进入后台运行", Toast.LENGTH_SHORT).show();
    }
}
